<script setup lang="ts">
import { ref } from "vue";
import { useMabangOrders } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { Search } from '@element-plus/icons-vue'
import { useI18n } from "vue-i18n";
const { t } = useI18n();
import { useRoute } from 'vue-router'
const route = useRoute()
import Refresh from "@iconify-icons/ep/refresh"

defineOptions({
  name: "MabangOrdersList"
});

const formRef = ref();
const tableRef = ref();
const {
  form, loading, columns,
  dataList, searchCondList, orderStatusList, shopOptionsList,
  // tab相关状态和方法
  activeTab, tabsList, tabCounts,
  // 详情抽屉相关方法
  openOrderDetail,
  // 其他方法
  sortChange, selectedNum, onSelectionCancel,
  onSearch, resetForm, openDialog,
  handleDelete, pagination, handleSizeChange, handleCurrentChange, handleSelectionChange
} = useMabangOrders(tableRef);

function onFullscreen() {
  tableRef.value.setAdaptive();
}
const size = ref<"small" | "default" | "large">("small");
</script>

<template>
  <div class="main">
    <el-form ref="formRef" :inline="true" :size="size" :model="form" class="search-form bg-bg_color w-[99/100] pl-4 pt-[12px] overflow-auto">

      <!-- 订单状态筛选 -->
      <el-form-item label="" prop="order_status">
        <el-select v-model="form.order_status" placeholder="订单状态" clearable class="!w-[100px]">
          <el-option v-for="(item) in orderStatusList" :label="item.name" :value="item.value" :key="item.value"/>
        </el-select>
      </el-form-item>

      <!-- 店铺筛选 -->
      <el-form-item label="" prop="shop_id">
        <el-select v-model="form.shop_id" placeholder="选择店铺" clearable class="!w-[150px]">
          <el-option v-for="(item) in shopOptionsList" :label="item.name" :value="item.value" :key="item.value"/>
        </el-select>
      </el-form-item>

      <!-- 搜索条件 -->
      <el-form-item prop="search">
        <el-input v-model="form.search" style="max-width: 600px;" clearable placeholder="请输入搜索内容" class="input-with-select">
          <template #prepend>
            <el-select v-model="form.search_field" placeholder="选择查询条件" style="width: 120px">
              <el-option v-for="(item) in searchCondList" :label="'按'+item.name" :value="item.value" :key="item.value"/>
            </el-select>
          </template>
        </el-input>
      </el-form-item>

      <!-- 时间筛选 -->
      <el-form-item prop="time">
        <el-select v-model="form.search_field2" placeholder="选择时间" class="search_field" style="width: 100px;border-radius:0">
          <el-option label="录入系统时间" value="created_at" />
          <el-option label="更新时间" value="updated_at" />
          <el-option label="支付时间" value="paid_time" />
        </el-select>
        <el-date-picker
          v-model="form.time" value-format="YYYY-MM-DD" format="YYYY-MM-DD"
          type="daterange" style="width: 200px;border-radius: 0px;border-left: 0;margin-left: -1px;"
          range-separator="~" prefix-icon=""
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" :icon="useRenderIcon('ri:search-line')" :loading="loading" @click="onSearch">{{ t("buttons.search") }}</el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)" style="margin-left:5px;">{{ t("buttons.reset") }}</el-button>
      </el-form-item>
    </el-form>

    <PureTableBar :title="'马帮订单管理'" :columns="columns" :tableRef="tableRef?.getTableRef()"
      @refresh="onSearch" @fullscreen="onFullscreen">
      <template #title>
        <el-tabs v-model="activeTab" class="bg-bg_color w-[99/100] pt-[1px] overflow-auto" style="margin-top:-8px;" @tab-click="onSearch">
          <el-tab-pane
            v-for="tab in tabsList"
            :key="tab.value"
            :name="tab.value"
            :label="`${tab.name}(${tabCounts[tab.value] || 0})`">
          </el-tab-pane>
          <div>
            <span v-if="selectedNum > 0" style="font-size: var(--el-font-size-base)" class="text-[rgba(42,46,54,0.5)] dark:text-[rgba(220,220,242,0.5)]">
              已选 {{ selectedNum }} 项
            </span>
          </div>
        </el-tabs>
      </template>

      <template v-slot="{ size, dynamicColumns }">
        <pure-table ref="tableRef" adaptive @sort-change="sortChange"
          :adaptiveConfig="{ offsetBottom: 70 }"
          align-whole="center" row-key="auto_id" showOverflowTooltip table-layout="auto"
          :loading="loading" :size="size" :data="dataList"
          :pagination="{ ...pagination, size }" :columns="dynamicColumns"
          :header-cell-style="{ background: 'var(--el-fill-color-light)',color: 'var(--el-text-color-primary)' }"
          :border="true" @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange">
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-table__inner-wrapper::before) {
  height: 0;
}
:deep(.search_field .el-select__wrapper) {
  border-radius: 0;
  border-right: 0;
  margin-right: -1px;
}
:deep(.el-form--inline .el-form-item) {
  margin-right: 8px;
}
:deep(.el-pagination) {
  margin: 3px 0 0 0 !important;
}
:deep(.el-tabs__header) {
  margin: 0 0 8px;
}
.main-content {
  margin: 15px 15px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 8px;
  }
}


</style>
